package com.educenter.controller;


import com.JwtUtils;
import com.R;
import com.educenter.entity.UcenterMember;
import com.educenter.entity.vo.RegisterVo;
import com.educenter.service.UcenterMemberService;
import com.ordervo.UcenterMemberOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-07-11
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

	@Autowired
	private UcenterMemberService memberService;

	//登录
	@PostMapping("/login")
	public R loginUser(@RequestBody UcenterMember member){
		String token = memberService.login(member);
		return R.ok().data("token",token);
	}

	//注册
	@PostMapping("/register")
	public R registerUser(@RequestBody RegisterVo registerVo){
		memberService.register(registerVo);
		return R.ok();
	}

	//根据token获取用户信息
	@GetMapping("/getMemberInfo")
	public R getMemberInfo(HttpServletRequest request){
		String memberId = JwtUtils.getMemberIdByJwtToken(request);
		UcenterMember member = memberService.getById(memberId);
		return R.ok().data("userInfo",member);

	}

	//评论需要查
	@PostMapping("/getCommentMemberById/{id}")
	public UcenterMember getMemberById(@PathVariable("id") String id){
		UcenterMember ucenterMember = memberService.getById(id);

		UcenterMember member = new UcenterMember();
		BeanUtils.copyProperties(ucenterMember,member);
		return member;
	}

	//支付需要查
	@PostMapping("/getUserInfoOrder/{id}")
	public UcenterMemberOrder getUserInfoOrder(@PathVariable("id") String id){
		UcenterMember ucenterMember = memberService.getById(id);
		UcenterMemberOrder member = new UcenterMemberOrder();
		BeanUtils.copyProperties(ucenterMember,member);
		return member;
	}


	@GetMapping("/countRegister/{day}")
	public R countRegister(@PathVariable String day){
		Integer count = memberService.countRegisterDay(day);
		return R.ok().data("countRegister",count);
	}

}

